<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章管理功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #333;
        }
        button {
            background: #4f46e5;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #3730a3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #4f46e5;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
        }
        .success {
            border-left-color: #28a745;
            background: #d4edda;
        }
        .warning {
            border-left-color: #ffc107;
            background: #fff3cd;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select, textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        .article-form {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin-bottom: 20px;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 10px;
            margin-top: 15px;
        }
        .stat-item {
            text-align: center;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .stat-number {
            font-size: 24px;
            font-weight: bold;
            color: #4f46e5;
        }
        .stat-label {
            font-size: 12px;
            color: #666;
        }
        .article-list {
            max-height: 400px;
            overflow-y: auto;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 10px;
        }
        .article-item {
            padding: 10px;
            border-bottom: 1px solid #eee;
            margin-bottom: 10px;
        }
        .article-item:last-child {
            border-bottom: none;
        }
        .article-title {
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
        }
        .article-meta {
            font-size: 12px;
            color: #666;
        }
    </style>
</head>
<body>
    <h1>文章管理功能测试</h1>
    
    <div class="container">
        <h2>API连接测试</h2>
        <button onclick="testApiConnection()">测试API连接</button>
        <div id="apiResult" class="result"></div>
    </div>

    <div class="container">
        <h2>文章统计信息</h2>
        <button onclick="getStatistics()">获取统计信息</button>
        <div id="statisticsResult" class="result"></div>
    </div>

    <div class="container">
        <h2>文章列表测试</h2>
        <button onclick="getArticles()">获取文章列表</button>
        <button onclick="getArticlesWithQuery()">条件查询文章</button>
        <div id="articlesResult" class="result"></div>
    </div>

    <div class="container">
        <h2>创建文章测试</h2>
        <div class="article-form">
            <div class="form-group">
                <label>文章标题:</label>
                <input type="text" id="articleTitle" placeholder="请输入文章标题">
            </div>
            <div class="form-group">
                <label>文章分类:</label>
                <select id="articleCategory">
                    <option value="tech">技术文章</option>
                    <option value="product">产品介绍</option>
                    <option value="news">公司新闻</option>
                    <option value="industry">行业动态</option>
                </select>
            </div>
            <div class="form-group">
                <label>文章摘要:</label>
                <textarea id="articleSummary" placeholder="请输入文章摘要"></textarea>
            </div>
            <div class="form-group">
                <label>文章内容:</label>
                <textarea id="articleContent" placeholder="请输入文章内容"></textarea>
            </div>
            <div class="form-group">
                <label>文章标签:</label>
                <input type="text" id="articleTags" placeholder="请输入标签，用逗号分隔">
            </div>
            <div class="form-group">
                <label>文章状态:</label>
                <select id="articleStatus">
                    <option value="draft">草稿</option>
                    <option value="published">发布</option>
                </select>
            </div>
        </div>
        <button onclick="createArticle()">创建文章</button>
        <div id="createResult" class="result"></div>
    </div>

    <div class="container">
        <h2>文章操作测试</h2>
        <div class="form-group">
            <label>文章ID:</label>
            <input type="text" id="articleId" placeholder="请输入要操作的文章ID">
        </div>
        <button onclick="getArticleById()">获取文章详情</button>
        <button onclick="publishArticle()">发布文章</button>
        <button onclick="offlineArticle()">下线文章</button>
        <button onclick="setTopStatus()">设置置顶</button>
        <button onclick="setRecommendStatus()">设置推荐</button>
        <button onclick="deleteArticle()">删除文章</button>
        <div id="operationResult" class="result"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:5095/api';
        
        // 测试API连接
        async function testApiConnection() {
            const resultDiv = document.getElementById('apiResult');
            resultDiv.innerHTML = '正在测试API连接...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/list?page=1&pageSize=1`);
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ API连接成功!</strong><br>
                            状态码: ${response.status}<br>
                            响应时间: ${response.headers.get('date')}<br>
                            数据: ${JSON.stringify(data, null, 2)}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ API连接失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 网络错误!</strong><br>
                        错误信息: ${error.message}<br>
                        请确保后端服务正在运行在 http://localhost:5095
                    </div>
                `;
            }
        }

        // 获取统计信息
        async function getStatistics() {
            const resultDiv = document.getElementById('statisticsResult');
            resultDiv.innerHTML = '正在获取统计信息...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/statistics`);
                const data = await response.json();
                
                if (response.ok) {
                    const stats = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 统计信息获取成功!</strong><br>
                            <div class="stats-grid">
                                <div class="stat-item">
                                    <div class="stat-number">${stats.totalArticles || 0}</div>
                                    <div class="stat-label">总文章数</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.publishedArticles || 0}</div>
                                    <div class="stat-label">已发布</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.draftArticles || 0}</div>
                                    <div class="stat-label">草稿</div>
                                </div>
                                <div class="stat-item">
                                    <div class="stat-number">${stats.totalViews || 0}</div>
                                    <div class="stat-label">总阅读量</div>
                                </div>
                            </div>
                            <details>
                                <summary>查看详细统计</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 统计信息获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 统计信息获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 获取文章列表
        async function getArticles() {
            const resultDiv = document.getElementById('articlesResult');
            resultDiv.innerHTML = '正在获取文章列表...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/list?page=1&pageSize=10`);
                const data = await response.json();
                
                if (response.ok) {
                    const articles = data.data.items || [];
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 文章列表获取成功!</strong><br>
                            共找到 ${data.data.totalCount} 篇文章<br>
                            <div class="article-list">
                                ${articles.map(article => `
                                    <div class="article-item">
                                        <div class="article-title">${article.title}</div>
                                        <div class="article-meta">
                                            作者: ${article.authorName} | 
                                            分类: ${article.categoryName} | 
                                            状态: ${article.status} | 
                                            阅读量: ${article.viewCount}
                                        </div>
                                    </div>
                                `).join('')}
                            </div>
                            <details>
                                <summary>查看详细数据</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 文章列表获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 文章列表获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 条件查询文章
        async function getArticlesWithQuery() {
            const resultDiv = document.getElementById('articlesResult');
            resultDiv.innerHTML = '正在条件查询文章...';
            
            try {
                const params = new URLSearchParams({
                    page: 1,
                    pageSize: 5,
                    status: 'published'
                });
                
                const response = await fetch(`${API_BASE_URL}/article/list?${params}`);
                const data = await response.json();
                
                if (response.ok) {
                    const articles = data.data.items || [];
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 条件查询成功!</strong><br>
                            查询条件: 已发布状态<br>
                            共找到 ${data.data.totalCount} 篇文章<br>
                            <div class="article-list">
                                ${articles.map(article => `
                                    <div class="article-item">
                                        <div class="article-title">${article.title}</div>
                                        <div class="article-meta">
                                            作者: ${article.authorName} | 
                                            分类: ${article.categoryName} | 
                                            状态: ${article.status} | 
                                            阅读量: ${article.viewCount}
                                        </div>
                                    </div>
                                `).join('')}
                            </div>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 条件查询失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 条件查询错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 创建文章
        async function createArticle() {
            const resultDiv = document.getElementById('createResult');
            resultDiv.innerHTML = '正在创建文章...';
            
            const title = document.getElementById('articleTitle').value;
            const categoryId = document.getElementById('articleCategory').value;
            const summary = document.getElementById('articleSummary').value;
            const content = document.getElementById('articleContent').value;
            const tags = document.getElementById('articleTags').value;
            const status = document.getElementById('articleStatus').value;
            
            if (!title || !content) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 请填写必填字段!</strong><br>
                        文章标题和内容为必填项
                    </div>
                `;
                return;
            }
            
            try {
                const requestData = {
                    title: title,
                    content: content,
                    summary: summary,
                    categoryId: categoryId,
                    tags: tags,
                    status: status,
                    authorId: '00000000-0000-0000-0000-000000000001', // 临时使用默认作者ID
                    coverImage: '',
                    seoTitle: title,
                    seoKeywords: tags,
                    seoDescription: summary,
                    sortWeight: 0
                };
                
                const response = await fetch(`${API_BASE_URL}/article/create`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(requestData)
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 文章创建成功!</strong><br>
                            标题: ${title}<br>
                            状态: ${status}<br>
                            <details>
                                <summary>查看响应详情</summary>
                                <pre>${JSON.stringify(data, null, 2)}</pre>
                            </details>
                        </div>
                    `;
                    
                    // 清空表单
                    document.getElementById('articleTitle').value = '';
                    document.getElementById('articleSummary').value = '';
                    document.getElementById('articleContent').value = '';
                    document.getElementById('articleTags').value = '';
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 文章创建失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 文章创建错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 获取文章详情
        async function getArticleById() {
            const articleId = document.getElementById('articleId').value;
            if (!articleId) {
                alert('请输入文章ID');
                return;
            }
            
            const resultDiv = document.getElementById('operationResult');
            resultDiv.innerHTML = '正在获取文章详情...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/${articleId}`);
                const data = await response.json();
                
                if (response.ok) {
                    const article = data.data;
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 文章详情获取成功!</strong><br>
                            <div class="article-item">
                                <div class="article-title">${article.title}</div>
                                <div class="article-meta">
                                    作者: ${article.authorName} | 
                                    分类: ${article.categoryName} | 
                                    状态: ${article.status} | 
                                    阅读量: ${article.viewCount} | 
                                    点赞数: ${article.likeCount}
                                </div>
                                <div>摘要: ${article.summary}</div>
                            </div>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 文章详情获取失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 文章详情获取错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 发布文章
        async function publishArticle() {
            const articleId = document.getElementById('articleId').value;
            if (!articleId) {
                alert('请输入文章ID');
                return;
            }
            
            const resultDiv = document.getElementById('operationResult');
            resultDiv.innerHTML = '正在发布文章...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/${articleId}/publish`, {
                    method: 'POST'
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 文章发布成功!</strong><br>
                            文章ID: ${articleId}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 文章发布失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 文章发布错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 下线文章
        async function offlineArticle() {
            const articleId = document.getElementById('articleId').value;
            if (!articleId) {
                alert('请输入文章ID');
                return;
            }
            
            const resultDiv = document.getElementById('operationResult');
            resultDiv.innerHTML = '正在下线文章...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/${articleId}/offline`, {
                    method: 'POST'
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 文章下线成功!</strong><br>
                            文章ID: ${articleId}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 文章下线失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 文章下线错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 设置置顶状态
        async function setTopStatus() {
            const articleId = document.getElementById('articleId').value;
            if (!articleId) {
                alert('请输入文章ID');
                return;
            }
            
            const resultDiv = document.getElementById('operationResult');
            resultDiv.innerHTML = '正在设置置顶状态...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/${articleId}/top?isTop=true`, {
                    method: 'POST'
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 置顶状态设置成功!</strong><br>
                            文章ID: ${articleId}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 置顶状态设置失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 置顶状态设置错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 设置推荐状态
        async function setRecommendStatus() {
            const articleId = document.getElementById('articleId').value;
            if (!articleId) {
                alert('请输入文章ID');
                return;
            }
            
            const resultDiv = document.getElementById('operationResult');
            resultDiv.innerHTML = '正在设置推荐状态...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/${articleId}/recommend?isRecommend=true`, {
                    method: 'POST'
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 推荐状态设置成功!</strong><br>
                            文章ID: ${articleId}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 推荐状态设置失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 推荐状态设置错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 删除文章
        async function deleteArticle() {
            const articleId = document.getElementById('articleId').value;
            if (!articleId) {
                alert('请输入文章ID');
                return;
            }
            
            if (!confirm(`确定要删除文章 ${articleId} 吗？此操作不可恢复！`)) {
                return;
            }
            
            const resultDiv = document.getElementById('operationResult');
            resultDiv.innerHTML = '正在删除文章...';
            
            try {
                const response = await fetch(`${API_BASE_URL}/article/${articleId}`, {
                    method: 'DELETE'
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    resultDiv.innerHTML = `
                        <div class="success">
                            <strong>✅ 文章删除成功!</strong><br>
                            文章ID: ${articleId}
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `
                        <div class="error">
                            <strong>❌ 文章删除失败!</strong><br>
                            状态码: ${response.status}<br>
                            错误信息: ${data.message || '未知错误'}
                        </div>
                    `;
                }
            } catch (error) {
                resultDiv.innerHTML = `
                    <div class="error">
                        <strong>❌ 文章删除错误!</strong><br>
                        错误信息: ${error.message}
                    </div>
                `;
            }
        }

        // 页面加载时自动测试API连接
        window.onload = function() {
            testApiConnection();
        };
    </script>
</body>
</html> 